from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait


class Base():
    # 初始化浏览器驱动
    def __init__(self, driver: WebDriver):
        self.driver = driver

    # 打开网址的方法
    def open_url(self, url):
        self.driver.get(url)

    # 查找元素的方法
    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 10, 0.5).until(EC.visibility_of_element_located(loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    # 点击方法
    def click(self, loc):
        self.find_element(loc).click()

    # 输入文本方法
    def send_keys(self, loc, info):
        self.find_element(loc).clear()
        self.find_element(loc).send_keys(info)

    # 鼠标悬停的方法
    def xuan(self, loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    # 下拉菜单选择,按value选择
    def xia(self, loc, value):
        select = Select(self.find_element(loc))
        select.select_by_value(value)

    # 获取文本值的方法
    def get_text(self, loc):
        return self.find_element(loc).text

    # 拖拽元素方法
    def tuo(self, x, y):
        ActionChains(self.driver).drag_and_drop(x, y).perform()
